/*
 * @Author: SuperficialL
 * @Date: 2020-07-09 21:34:40
 * @LastEditTime: 2021-03-15 16:35:59
 * @Description: 缓存
 */
import { ref, watchEffect } from "vue";

export const get = (key) => localStorage.getItem(key);
export const set = (key, data) => localStorage.setItem(key, data);
export const remove = (key) => localStorage.removeItem(key);

export const setJSON = (key, data) => set(key, JSON.stringify(data))
export const getJSON = (key) => {
  const data = get(key)
  return typeof data === 'string' ? JSON.parse(data) : null
};


export const getAccessor = (key, initState) => {
  const localState = ref(getJSON(key) || initState)
  watchEffect(() => setJSON(key, localState.value))
  return localState
}